package com.dbsop.operation.query.transport;

import org.elasticsearch.action.admin.indices.analyze.AnalyzeAction;
import org.elasticsearch.action.admin.indices.analyze.AnalyzeRequestBuilder;
import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

public class TcAnalyzeRequest {

    private String[] texts;

    private String analyze;

    private AnalyzeRequestBuilder analyzeRequest;

    public TcAnalyzeRequest(String analyze , String... texts ) {
        this.texts = texts;
        this.analyze = analyze;
//        initAnalyzeRequest();
    }
//
//    private void initAnalyzeRequest(){
//        analyzeRequest = new AnalyzeRequestBuilder(client, AnalyzeAction.INSTANCE);
//        analyzeRequest.setText(texts);
//        analyzeRequest.setAnalyzer(analyze);
//    }


    public Set<String> analyzeRequest(){
        Set<String> terms = new LinkedHashSet<>();
        for (String s : texts) {
            terms.add(s);
        }
        Iterator<AnalyzeResponse.AnalyzeToken> iterator = this.analyzeRequest.execute().actionGet().getTokens().iterator();
        while (iterator.hasNext()){
            AnalyzeResponse.AnalyzeToken analyzeToken = iterator.next();
            String term = analyzeToken.getTerm();
            terms.add(term);
        }
        return terms;
    }


}
